﻿using llm_net.Models;
using llm_net.Services.Abstractions;
using llm_project.Models;
using Microsoft.AspNetCore.Mvc;

namespace llm_net.Controllers
{
    [Route("api/[controller]/[action]")]
    [ApiController]
    public class UserTagController: ControllerBase
    {
        private readonly IUserTagService _userTagService;
        // 依赖注入：通过构造函数注入业务层接口
        public UserTagController(IUserTagService userTagService)
        {
            _userTagService = userTagService;
        }
        /// <summary>
        /// 提交用户行为生成标签
        /// </summary>
        /// <param name="actions">用户行为列表</param>
        /// <returns>标签与权重</returns>
        [HttpPost]
        public async Task<IActionResult> GenerateTags(UserBehaviorReq behavior)
        {
            // 1. 验证输入（控制层职责：参数校验）
            if (behavior.Actions == null || !behavior.Actions.Any())
            {
                return BadRequest("用户行为列表不能为空");
            }

            try
            {
                // 3. 调用业务层处理（控制层不做业务逻辑）
                var tags = await _userTagService.GenerateUserTagsAsync(behavior);

                // 4. 返回结果（控制层职责：包装响应）
                return Ok(new
                {
                    Success = true,
                    Data = tags,
                    Message = "标签生成成功"
                });
            }
            catch (Exception ex)
            {
                return StatusCode(500, new
                {
                    Success = false,
                    Message = $"生成标签失败：{ex.Message}"
                });
            }
        }
        [HttpPost]
        public async Task<IActionResult> GenerateBatchTags(BatchUserBehaviorReq batchRequest)
        {
            if (batchRequest?.UserBehaviors == null || !batchRequest.UserBehaviors.Any())
            {
                return BadRequest("用户行为列表不能为空");
            }

            try
            {
                var results = await _userTagService.GenerateBatchUserTagsAsync(batchRequest);

                return Ok(new
                {
                    Success = true,
                    Data = results,
                    Message = $"成功处理 {results.Count} 个用户"
                });
            }
            catch (Exception ex)
            {
                return StatusCode(500, new
                {
                    Success = false,
                    Message = $"批量处理失败：{ex.Message}"
                });
            }
        }
        /// <summary>
        /// 获取全局标签池
        /// </summary>
        [HttpGet]
        public IActionResult GetGlobalTags()
        {
            var tags = _userTagService.GetGlobalTagPool();
            return Ok(new
            {
                Success = true,
                Data = tags,
                Count = tags.Count
            });
        }
    }
}
